package com.campus.controller;

import com.campus.common.CampusWall;
import com.campus.common.Result;
import com.campus.entity.Comments;
import com.campus.service.CommentService;
import com.campus.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class CommentController {
    
    @Autowired
    private TokenUtils tokenUtils;
    
    @Autowired
    private CommentService commentService;
    
    /**
     * 发表评论
     *
     * @param postId 帖子ID
     * @param comment 评论内容
     * @param token 用户Token
     * @return
     */
    @PostMapping("/post/{postId}/comment")
    public Result createComment(@PathVariable("postId") String postId, 
                               @RequestBody Comments comment,
                               @RequestHeader(CampusWall.CAMPUS_TOKEN) String token) {
        Result result = commentService.createComment(token, postId, comment);
        return result;
    }
    
    /**
     * 获取帖子评论列表
     *
     * @param postId 帖子ID
     * @return
     */
    @GetMapping("/post/{postId}/comments")
    public Result getComments(@PathVariable("postId") String postId) {
        Result result = commentService.getCommentsByPostId(postId);
        return result;
    }
    
    /**
     * 点赞评论
     *
     * @param commentId 评论ID
     * @param token 用户Token
     * @return
     */
    @PostMapping("/comment/{commentId}/like")
    public Result likeComment(@PathVariable("commentId") String commentId,
                             @RequestHeader(CampusWall.CAMPUS_TOKEN) String token) {
        Result result = commentService.likeComment(token, commentId);
        return result;
    }
    
    /**
     * 取消点赞评论
     *
     * @param commentId 评论ID
     * @param token 用户Token
     * @return
     */
    @PostMapping("/comment/{commentId}/unlike")
    public Result unlikeComment(@PathVariable("commentId") String commentId,
                               @RequestHeader(CampusWall.CAMPUS_TOKEN) String token) {
        Result result = commentService.unlikeComment(token, commentId);
        return result;
    }
    
    /**
     * 检查评论点赞状态
     *
     * @param commentId 评论ID
     * @param token 用户Token
     * @return 1表示已点赞，0表示未点赞
     */
    @GetMapping("/comment/{commentId}/check-like")
    public Result checkCommentLikeStatus(@PathVariable("commentId") String commentId,
                                        @RequestHeader(CampusWall.CAMPUS_TOKEN) String token) {
        Result result = commentService.checkCommentLikeStatus(token, commentId);
        return result;
    }
}